AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( "shared.lua" )

function ENT:Initialize()
	-- Set model.
	self.Entity:SetModel( self:GetKeyValue( "model", "models/items/ammocrate_smg1.mdl" ) )
	
	-- Initialise physics.
	self.Entity:PhysicsInitBox( self:OBBMins(), self:OBBMaxs() )
	self.Entity:SetCollisionBounds( self:OBBMins(), self:OBBMaxs() )
	
	// Custom physics makes entity a dull boy. I mean, wrongly orientated.
	if ( self:GetKeyValue( "angles", -1 ) != -1 ) then
		local angle = {}
		for k, v in pairs( string.Explode( " ", self:GetKeyValue( "angles" ) ) ) do
			angle[ k ] = tonumber( v ) or 0
		end
		self:SetAngles( Angle( unpack( angle ) ) )
	end
	
	-- Freeze!
	local phys = self:GetPhysicsObject()
	if ( phys:IsValid() ) then
		phys:EnableMotion( false )
	end
	
	self:SetUseType( SIMPLE_USE )
end

function ENT:Use( ply )
	if GAMEMODE:CallHook ("DisallowArmoryUse", ply, self.Entity) then return end
	GAMEMODE:CallHook( "ArmoryUsed", ply, self.Entity )
	
	local seq = self:LookupSequence( "Close" )
	if ( seq ) then self:SetSequence( seq ) end
	self.Close = CurTime() + 3
end
function ENT:Think()
	if ( self.Close && CurTime() > self.Close ) then
		local seq = self:LookupSequence( "Open" )
		if ( seq ) then self:SetSequence( seq ) end
		self.Close = nil
	end
	
	self:SetPlaybackRate( 0.75 )
	self:NextThink( CurTime() )
	return true
end

function ENT:OnTakeDamage( dmg )
	gamemode.Call( "EntityTakeDamage", self, dmg )
end
	
function ENT:GetKeyValue( key, default )
	if ( !self.KeyValues || !self.KeyValues[ key ] ) then return default end
	return self.KeyValues[ key ]
end
function ENT:KeyValue( key, value )
	self.KeyValues = self.KeyValues or {}
	self.KeyValues[ key ] = value
end
